package testcases.common;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Before;

import com.roadmap.common.RdcLogger;
import com.roadmap.entities.Aspirant;
import com.roadmap.entities.Execution;
import com.roadmap.entities.LifeTask;
import com.roadmap.exception.ServerException;
import com.roadmap.repo.db.DatabaseUtility;
import com.roadmap.repo.db.IDatabaseUtility;

public class BaseTest {
	protected final IDatabaseUtility _dbUtil = new DatabaseUtility();
	protected final Logger _log = RdcLogger.getLogger(this.getClass());

	protected Aspirant _currentUser;
	protected Aspirant _itemOwner;

	@Before
	public void setUp() {
		String hql = "from Aspirant where mail=? or mail=?";
		try {
			List list = _dbUtil.executeHQLQuery(hql, new Object[] { TestConst.Current_User_Mail,
					TestConst.Item_Owner_Mail });
			Iterator iter = list.iterator();
			while (iter.hasNext()) {
				Aspirant user = (Aspirant) iter.next();
				if (user.getMail().equals(TestConst.Current_User_Mail)) {
					_currentUser = user;
				}
				else if (user.getMail().equals(TestConst.Item_Owner_Mail)) {
					_itemOwner = user;
				}
			}
		}
		catch (ServerException e) {
			_log.fatal(e);
		}
	}

	protected LifeTask obtainTaskWithoutPlan() {
		LifeTask task = new LifeTask();
		task.setAspirantId(_itemOwner.getId());
		task.setContent("task content");
		return task;
	}

	protected LifeTask obtainTaskWithPlan(String planId) {
		LifeTask task = new LifeTask();
		task.setGoalId(planId);
		task.setAspirantId(_itemOwner.getId());
		task.setContent("task content");
		return task;
	}

	protected Execution obtainExec(String taskId) {
		Execution exec = new Execution();
		exec.setTaskId(taskId);
		exec.setContent("exec content");
		exec.setExecuteDate(new Date());
		exec.setExecuteTimeLength(10);
		exec.setProportionOfTask(10);
		return exec;
	}
}
